package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.handler.JwtHelper;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xin Zhou
 * @since 2024-01-27
 */
@RestController
@RequestMapping("/user/userinfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
//  /user/userinfo/update
    @ApiOperation("保存用户信息")
    @PutMapping("/update")
    public R save(@RequestHeader(value = "token",required = false) String token, UserAuthVo userAuthVo){
        Long userId = JwtHelper.getUserId(token);

        UserInfo userInfo = new UserInfo();

        userInfo.setId(userId);
        userInfo.setName(userAuthVo.getName());
        userInfo.setCertificatesType(userAuthVo.getCertificatesType());
        userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
        userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());

        userInfoService.updateById(userInfo);
        return R.ok();
    }

    /*
    * @RequestBody 注解可以接收前端的JSON数据
    * */
    @ApiOperation("用户登录功能")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
      Map<String,Object> resultMap = userInfoService.login(loginVo);
      return R.ok().data(resultMap);
    }

    /*
    * @RequestHeader 获取请求头的信息
    * */
    @ApiOperation("通过tokenId获取用户信息")
    @GetMapping("/info")
    public R getUserInfo(@RequestHeader(value = "token",required = false) String token){
        Long userId = JwtHelper.getUserId(token);
        UserInfo resultUser = userInfoService.getuserInfo(userId);
//        返回用户信息
        return R.ok().data("user",resultUser);
    }

}

